//
//  CWHomeCellTopView.swift
//  SinaBlog
//
//  Created by ChenWei on 15/11/30.
//  Copyright © 2015年 cw. All rights reserved.
//

import UIKit

class CWHomeCellTopView: UIView {

    var status: Status? {
        didSet{
            // 1.设置头像
            iconView.sd_setImageWithURL(status?.user?.profile_URL)
            // 2.设置认证图标
            verifiedView.image = status?.user?.verified_image
            // 3.设置昵称
            nameLabel.text = status?.user?.name
            // 4.设置vip图标
            if let image = status?.user?.vipImage {
                vipView.image = image
                // 修改昵称颜色
                nameLabel.textColor = UIColor.orangeColor()
            }else {
                vipView.image = nil
                nameLabel.textColor = UIColor.lightGrayColor()
            }
            // 5.设置时间
            timeLabel.text = status?.created_at
            
            // 6.设置来源
            sourceLabe.text = status?.source
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 1.添加子控件
        addSubview(iconView)
        iconView.addSubview(verifiedView)
        addSubview(nameLabel)
        addSubview(vipView)
        addSubview(timeLabel)
        addSubview(sourceLabe)
        
        // 2.布局子控件
        // 2.1布局头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(10)
            make.left.equalTo(10)
            make.size.equalTo(CGSizeMake(50, 50))
            make.bottom.equalTo(self).offset(-10)
        }
        // 2.2布局认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(-8)
            make.bottom.equalTo(iconView).offset(8)
        }
        // 2.3布局昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(20)
        }
        // 2.4布局会员图标
        vipView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(10)
            make.top.equalTo(nameLabel)
            make.size.equalTo(CGSizeMake(17, 17))
        }
        // 2.5布局时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_right)
            make.left.equalTo(nameLabel)
        }
        // 2.6布局来源
        sourceLabe.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(10)
            make.bottom.equalTo(iconView)
        }
    }
    
    // MARK: - 懒加载
    /// 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
    
    /// 昵称
    private lazy var nameLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.text = "小码哥"
        nameLabel.textColor = UIColor.orangeColor()
        nameLabel.font = UIFont.systemFontOfSize(20)
        return nameLabel
    }()
    
    /// 会员图标
    private lazy var vipView: UIImageView = {
        let vipView = UIImageView()
        vipView.image = UIImage(named: "common_icon_membership_expired")
        
        return vipView
    }()
    
    /// 时间
    private lazy var timeLabel: UILabel = {
        let timeLabel = UILabel()
        timeLabel.text = "刚刚"
        timeLabel.textColor = UIColor.orangeColor()
        timeLabel.font = UIFont.systemFontOfSize(15)
        return timeLabel
    }()
    
    /// 来源
    private lazy var sourceLabe: UILabel = {
        let sourceLabe = UILabel()
        sourceLabe.text = "来自: 人民网"
        sourceLabe.textColor = UIColor.lightGrayColor()
        sourceLabe.font = UIFont.systemFontOfSize(15)
        sourceLabe.sizeToFit()
        return sourceLabe
    }()
}
